﻿#include "dlgmain.h"
#include "ui_dlgmain.h"

static void FUNCALL fnUICommBridge(UI_Comm_Bridge *pData, void *userdata)
{
    DlgMain* usd = (DlgMain*)userdata;
    if(!usd)
        return;
//    qDebug() << "DlgMain~ fnUICommBridge msgtype=" << pData->msgtype << "data=" << pData->data;
    if(cb_ui_type_mantest == pData->msgtype){
        qDebug() << u8"cbUI接收到man线程收到消息, 线程ID=" << QThread::currentThreadId();
        QThread::sleep(20);
//        emit usd->sigSalesmantest();
        qDebug() << "sleep finish";
    }else if(cb_ui_type_womantest == pData->msgtype){
        qDebug() << u8"cbUI接收到woman线程收到消息, 线程ID=" << QThread::currentThreadId();
    }
}

DlgMain::DlgMain(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgMain)
{
    ui->setupUi(this);

    qDebug() << "DlgMain thread id=" << QThread::currentThreadId();
    Init();
}

DlgMain::~DlgMain()
{
    delete ui;
}

void DlgMain::Init()
{
    g_pFramework->SetCBUICommBridge(fnUICommBridge, this);
    g_pFramework->GetSalesman(&m_pSalesman);
    g_pFramework->GetSaleswoman(&m_pSaleswoman);

    connect(this,&DlgMain::sigSalesmantest,this,&DlgMain::slotSalesmantest);
}

void DlgMain::UnInit()
{

}

void DlgMain::slotSalesmantest()
{
    qDebug() << "slotSalesmantest()=" << QThread::currentThreadId();
}

void DlgMain::on_Btn_performanceTest_clicked()
{
    QString data = "hello world!";
    m_pSalesman->PostThreadMsg(trd_salesman_test, data);
    if(!m_timer.isActive()){
        disconnect(&m_timer);
        connect(&m_timer,&QTimer::timeout,[=](){
            static int s_count = 0;
            s_count++;
            QString szLog = QString("main test msg timer count=%1,----------------").arg(s_count);
            g_pLogger->WriteLog(szLog);
        });
        m_timer.start(600);
    }
}

void DlgMain::on_btn_dataparse_clicked()
{
    QJsonObject obj;
    obj.insert("cmd", "circle_test");
    obj.insert("tts","hello world");
    QJsonDocument doc;
    doc.setObject(obj);
    QString msg = QString::fromUtf8(doc.toJson(QJsonDocument::Compact));
    m_pSalesman->PostThreadMsg(message_circle_salesman_test, msg);
}

void DlgMain::on_btnManSendTest_clicked()
{
    QString data = QString(u8"向Man线程发送处理消息,线程ID=");
    qDebug() << data.toUtf8().data() << QThread::currentThreadId();
    m_pSalesman->PostThreadMsg(message_cbuitest_salesman, data);
}

void DlgMain::on_btnWomanSendTest_clicked()
{
    QString data = QString(u8"向Woman线程发送处理消息,线程ID");
    qDebug() << data.toUtf8().data() << QThread::currentThreadId();
    m_pSaleswoman->PostThreadMsg(message_cbuitest_saleswoman, data);
}
